package com.fang.networkdisk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.common.common.CommonPage;
import com.fang.common.exception.ExceptionCode;
import com.fang.common.exception.NetworkDiskException;
import com.fang.networkdisk.entity.bo.ReqPathPermitRelationBo;
import com.fang.networkdisk.entity.po.TbNetDisReqPathPermitRelation;
import com.fang.networkdisk.entity.vo.ReqPathPermitRelationVo;
import com.fang.networkdisk.mapper.TbNetDisReqPathPermitRelationMapper;
import com.fang.networkdisk.service.TbNetDisReqPathPermitRelationService;
import com.fang.networkdisk.util.NDBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 路径权限关联表,当一个接口需要多个权限的时候需要此表。与gateway_request_path表联合使用 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2024-12-07
 */
@Service
public class TbNetDisReqPathPermitRelationServiceImpl extends ServiceImpl<TbNetDisReqPathPermitRelationMapper, TbNetDisReqPathPermitRelation> implements TbNetDisReqPathPermitRelationService {

    @Resource
    private TbNetDisReqPathPermitRelationMapper tbNetDisReqPathPermitRelationMapper;

    @Override
    public CommonPage queryReqPathPermitRelation(ReqPathPermitRelationBo reqPathPermitRelationBo) {
//        QueryWrapper<TbNetDisReqPathPermitRelation> wrapper = new QueryWrapper<>();
//        wrapper.eq(IntegerUtils.isNotEmpty(reqPathPermitRelationBo.getId()), "id", reqPathPermitRelationBo.getId())
//                .likeRight(IntegerUtils.isNotEmpty(reqPathPermitRelationBo.getUrlId()), "url_id", reqPathPermitRelationBo.getUrlId())
//                .eq(IntegerUtils.isNotEmpty(reqPathPermitRelationBo.getPermissionId()), "permission_id", reqPathPermitRelationBo.getPermissionId())
//                .likeRight(StringUtils.isNotEmpty(reqPathPermitRelationBo.getUpdatedReason()), "updated_reason", reqPathPermitRelationBo.getUpdatedReason())
//                .eq(StringUtils.isNotEmpty(reqPathPermitRelationBo.getCreateUser()), "create_user", reqPathPermitRelationBo.getCreateUser())
//                .eq(StringUtils.isNotEmpty(reqPathPermitRelationBo.getUpdateUser()), "update_user", reqPathPermitRelationBo.getUpdateUser())
//                .apply(DateUtils.isNotEmpty(reqPathPermitRelationBo.getCreatedTime()), "date_format(created_time, '%Y-%m-%d') = {0}", DateUtils.getDateString(reqPathPermitRelationBo.getCreatedTime()));
//        Page<TbNetDisReqPathPermitRelation> page = new Page<>(reqPathPermitRelationBo.getCurPage(), reqPathPermitRelationBo.getPageSize());
//        this.tbNetDisReqPathPermitRelationMapper.selectPage(page, wrapper);
//        List<PermissionVo> resultList = NDBeanUtils.copyListProperties(page.getRecords(), PermissionVo.class);
//        CommonPage result = new CommonPage(reqPathPermitRelationBo.getCurPage(), page.getTotal(), resultList);
        reqPathPermitRelationBo.setOffset((reqPathPermitRelationBo.getCurPage() - 1) * reqPathPermitRelationBo.getPageSize());
        List<ReqPathPermitRelationVo> reqPathPermitRelationVos = this.tbNetDisReqPathPermitRelationMapper.queryReqPathPermitRelation(reqPathPermitRelationBo);
        int total = this.tbNetDisReqPathPermitRelationMapper.queryReqPathPermitRelationCount(reqPathPermitRelationBo);
        CommonPage result = new CommonPage(reqPathPermitRelationBo.getCurPage(), total, reqPathPermitRelationVos);
        return result;
    }

    @Override
    public TbNetDisReqPathPermitRelation findReqPathPermitRelationByUrlIdPermissionId(Integer urlId, Integer permissionId) {
        QueryWrapper<TbNetDisReqPathPermitRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("url_id", urlId).
                eq("permission_id", permissionId);
        return tbNetDisReqPathPermitRelationMapper.selectOne(wrapper);
    }

    @Override
    public int addReqPathPermitRelation(ReqPathPermitRelationBo reqPathPermitRelationBo) {
        TbNetDisReqPathPermitRelation db_reqPathPermitRelation = this.findReqPathPermitRelationByUrlIdPermissionId(reqPathPermitRelationBo.getUrlId(), reqPathPermitRelationBo.getPermissionId());
        if (NDBeanUtils.isObjectNotEmpty(db_reqPathPermitRelation)) {
            throw new NetworkDiskException(ExceptionCode.DATA_DUPLICATE);
        }
        TbNetDisReqPathPermitRelation reqPathPermitRelation = new TbNetDisReqPathPermitRelation();
        NDBeanUtils.copyListProperties(reqPathPermitRelationBo, reqPathPermitRelation);
        return tbNetDisReqPathPermitRelationMapper.insert(reqPathPermitRelation);
    }

    @Override
    public int updateReqPathPermitRelationById(ReqPathPermitRelationBo reqPathPermitRelationBo) {
        TbNetDisReqPathPermitRelation reqPathPermitRelation = new TbNetDisReqPathPermitRelation();
        NDBeanUtils.copyListProperties(reqPathPermitRelationBo, reqPathPermitRelation);
        return tbNetDisReqPathPermitRelationMapper.updateById(reqPathPermitRelation);
    }

    @Override
    public int deleteReqPathPermitRelationById(Integer id, String authorId) {
        return this.tbNetDisReqPathPermitRelationMapper.deleteReqPathPermitRelationById(id, authorId, new Date());
    }
}
